package com.tim.app.ui.adapter.search;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.tim.app.R;
import com.tim.app.server.entry.Problem;
import com.tim.app.util.TextUtil;

import java.util.List;


/**
 * 问题
 */
public class SearchProblemAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Problem> {

    private Context mContext;

    private String keyWord = "";

    public SearchProblemAdapter(Context mContext, List<Problem> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_search_problem, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, Problem data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvInfoTitle.setVisibility(View.VISIBLE);
            TextUtil.setTextHighLight(holder.tvInfoTitle, getKeyWord(), data.getTitle());
        }else{
            holder.tvInfoTitle.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvInfoDesc.setVisibility(View.VISIBLE);
            TextUtil.setTextHighLight(holder.tvInfoDesc, getKeyWord(), data.getContent());
        }else{
            holder.tvInfoDesc.setVisibility(View.GONE);
        }
        holder.tvSearchAnswerNumber.setText(mContext.getString(R.string.problemAnswerNumber, String.valueOf(data.getAnswerCount())));
        holder.tvSearchFollowNumber.setText(mContext.getString(R.string.problemFollowNumber, String.valueOf(data.getConcernCount())));
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        TextView tvInfoTitle, tvInfoDesc;
        TextView tvSearchAnswerNumber, tvSearchFollowNumber;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            tvInfoTitle = (TextView) itemView.findViewById(R.id.tvInfoTitle);
            tvInfoDesc = (TextView) itemView.findViewById(R.id.tvInfoDesc);
            tvSearchAnswerNumber = (TextView) itemView.findViewById(R.id.tvSearchAnswerNumber);
            tvSearchFollowNumber = (TextView) itemView.findViewById(R.id.tvSearchFollowNumber);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
